import extnames from '../dependencies/extnames.mjs';
import dirs from '../dependencies/dirs.mjs';
import path from 'path';

export default function (args, config) {
    if (!config.module) { config.module = {}; }
    if (!config.module.rules) { config.module.rules = []; }

    const rule = {
        test: new RegExp(extnames.js.map(ext => `(${ext}$)`).join('|')),
        exclude: /node_modules/,
        use: [
            {
                loader: path.join(dirs.root, 'dependencies', 'replace_import.mjs'),
            },
        ],
    };
    config.module.rules.push(rule);

    if (args.babel === true) {
        rule.use.push(
            {
                loader: 'babel-loader',
                options: {
                    presets: [
                        "@babel/preset-env",
                    ],
                    plugins: [],
                },
            },
        );
    }

    if (!config.resolve) { config.resolve = {}; }
    if (!config.resolve.extensions) { config.resolve.extensions = []; }
    extnames.js.forEach(t => {
        if (config.resolve.extensions.indexOf(t) < 0) {
            config.resolve.extensions.push(t);
        }
    });
};
